package com.peng.dian.utils;

import com.google.gson.Gson;
import com.peng.dian.pojo.audioEntity;
import com.peng.dian.pojo.pictureEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class commonUtils {

    public static void listFiles(File folder, ArrayList<Object>audio, ArrayList<Object>picture) {


        // 获取文件夹中的所有文件和子文件夹
        File[] files = folder.listFiles();

        // 遍历文件和子文件夹
        if (files != null) {
            for (File file : files) {
                // 如果是文件，则打印文件路径
                if (file.isFile()) {
                    //System.out.println("文件: " + file.getAbsolutePath());

                    if (isImage(file.getAbsolutePath())){
                        //图片
                        pictureEntity pictureEntity = new pictureEntity();
                        pictureEntity.setName(file.getAbsoluteFile().toString());
                        Gson gson = new Gson();
                        String json = gson.toJson(pictureEntity);
                        picture.add(json);
                    }
                    if (isAudio(file.getAbsolutePath())){
                        //音频
                        audioEntity audioEntity = new audioEntity();
                        audioEntity.setName(file.getAbsoluteFile().toString());
                        Gson gson = new Gson();
                        String json = gson.toJson(audioEntity);
                        audio.add(json);
                        //System.out.println(json);

                    }
                }
                // 如果是目录，则递归调用 listFiles 方法
                else if (file.isDirectory()) {
                    listFiles(file,audio,picture);
                }
            }
        }
    }

    // 判断是否图片文件
    public static boolean isImage(String filename) {
        String regex = "(?i).+\\.(jpg|jpeg|png|gif|bmp)$"; // 忽略大小写
        return filename.matches(regex);
    }

    // 判断是否音频文件
    public static boolean isAudio(String filename) {
        String regex = "(?i).+\\.(m4a|mp3|wav|flac|aac|ogg)$"; // 忽略大小写
        return filename.matches(regex);
    }
}
